<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Rails::SecretKeyGenerator</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Rails::SecretKeyGenerator</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/railties/lib/rails_generator/secret_key_generator_rb.html">
                vendor/rails/railties/lib/rails_generator/secret_key_generator.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001597">generate_secret</a>&nbsp;&nbsp;
      <a href="#M001600">generate_secret_with_openssl</a>&nbsp;&nbsp;
      <a href="#M001602">generate_secret_with_prng</a>&nbsp;&nbsp;
      <a href="#M001599">generate_secret_with_secure_random</a>&nbsp;&nbsp;
      <a href="#M001601">generate_secret_with_urandom</a>&nbsp;&nbsp;
      <a href="#M001598">generate_secret_with_win32_api</a>&nbsp;&nbsp;
      <a href="#M001596">new</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">


    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">GENERATORS</td>
          <td>=</td>
          <td class="context-item-value">[ :secure_random, :win32_api, :urandom, :openssl, :prng ].freeze</td>
        </tr>
        </table>
      </div>
    </div>



      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001596" class="method-detail">
        <a name="M001596"></a>

        <div class="method-heading">
          <a href="#M001596" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(identifier)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001596-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001596-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 12</span>
12:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">identifier</span>)
13:       <span class="ruby-ivar">@identifier</span> = <span class="ruby-identifier">identifier</span>
14:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001597" class="method-detail">
        <a name="M001597"></a>

        <div class="method-heading">
          <a href="#M001597" class="method-signature">
          <span class="method-name">generate_secret</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key with the best possible method available on the
current platform.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001597-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001597-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 18</span>
18:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret</span>
19:       <span class="ruby-identifier">generator</span> = <span class="ruby-constant">GENERATORS</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">g</span><span class="ruby-operator">|</span>
20:         <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;supports_#{g}?&quot;</span>)
21:       <span class="ruby-keyword kw">end</span>
22:       <span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;generate_secret_with_#{generator}&quot;</span>)
23:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001600" class="method-detail">
        <a name="M001600"></a>

        <div class="method-heading">
          <a href="#M001600" class="method-signature">
          <span class="method-name">generate_secret_with_openssl</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key with OpenSSL. If OpenSSL is not already
loaded, then this method will attempt to load it. LoadError will be raised
if that fails.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001600-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001600-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 68</span>
68:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret_with_openssl</span>
69:       <span class="ruby-identifier">require</span> <span class="ruby-value str">'openssl'</span>
70:       <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-value str">&quot;/dev/urandom&quot;</span>)
71:         <span class="ruby-comment cmt"># OpenSSL transparently seeds the random number generator with</span>
72:         <span class="ruby-comment cmt"># data from /dev/urandom. On platforms where that is not</span>
73:         <span class="ruby-comment cmt"># available, such as Windows, we have to provide OpenSSL with</span>
74:         <span class="ruby-comment cmt"># our own seed. Unfortunately there's no way to provide a</span>
75:         <span class="ruby-comment cmt"># secure seed without OS support, so we'll have to do with</span>
76:         <span class="ruby-comment cmt"># rand() and Time.now.usec().</span>
77:         <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Random</span>.<span class="ruby-identifier">seed</span>(<span class="ruby-identifier">rand</span>(<span class="ruby-value">0</span>).<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">usec</span>.<span class="ruby-identifier">to_s</span>)
78:       <span class="ruby-keyword kw">end</span>
79:       <span class="ruby-identifier">data</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">BN</span>.<span class="ruby-identifier">rand</span>(<span class="ruby-value">2048</span>, <span class="ruby-value">-1</span>, <span class="ruby-keyword kw">false</span>).<span class="ruby-identifier">to_s</span>
80:       <span class="ruby-keyword kw">return</span> <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span><span class="ruby-operator">::</span><span class="ruby-constant">SHA512</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">data</span>).<span class="ruby-identifier">hexdigest</span>
81:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001602" class="method-detail">
        <a name="M001602"></a>

        <div class="method-heading">
          <a href="#M001602" class="method-signature">
          <span class="method-name">generate_secret_with_prng</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key with Ruby&#8216;s pseudo random number
generator, as well as some environment information.
</p>
<p>
This is the least cryptographically secure way to generate a secret key,
and should be avoided whenever possible.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001602-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001602-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 94</span>
 94:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret_with_prng</span>
 95:       <span class="ruby-identifier">require</span> <span class="ruby-value str">'digest/sha2'</span>
 96:       <span class="ruby-identifier">sha</span> = <span class="ruby-constant">Digest</span><span class="ruby-operator">::</span><span class="ruby-constant">SHA2</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">512</span>)
 97:       <span class="ruby-identifier">now</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
 98:       <span class="ruby-identifier">sha</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_s</span>
 99:       <span class="ruby-identifier">sha</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">String</span>(<span class="ruby-identifier">now</span>.<span class="ruby-identifier">usec</span>)
100:       <span class="ruby-identifier">sha</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">String</span>(<span class="ruby-identifier">rand</span>(<span class="ruby-value">0</span>))
101:       <span class="ruby-identifier">sha</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">String</span>(<span class="ruby-identifier">$$</span>)
102:       <span class="ruby-identifier">sha</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-ivar">@identifier</span>
103:       <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">sha</span>.<span class="ruby-identifier">hexdigest</span>
104:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001599" class="method-detail">
        <a name="M001599"></a>

        <div class="method-heading">
          <a href="#M001599" class="method-signature">
          <span class="method-name">generate_secret_with_secure_random</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key with Ruby 1.9&#8216;s SecureRandom module.
Raises LoadError if the current Ruby version does not support SecureRandom.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001599-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001599-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 60</span>
60:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret_with_secure_random</span>
61:       <span class="ruby-identifier">require</span> <span class="ruby-value str">'securerandom'</span>
62:       <span class="ruby-keyword kw">return</span> <span class="ruby-constant">SecureRandom</span>.<span class="ruby-identifier">hex</span>(<span class="ruby-value">64</span>)
63:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001601" class="method-detail">
        <a name="M001601"></a>

        <div class="method-heading">
          <a href="#M001601" class="method-signature">
          <span class="method-name">generate_secret_with_urandom</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key with /dev/urandom. Raises SystemCallError on
failure.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001601-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001601-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 85</span>
85:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret_with_urandom</span>
86:       <span class="ruby-keyword kw">return</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-value str">&quot;/dev/urandom&quot;</span>, <span class="ruby-value">64</span>).<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)[<span class="ruby-value">0</span>]
87:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001598" class="method-detail">
        <a name="M001598"></a>

        <div class="method-heading">
          <a href="#M001598" class="method-signature">
          <span class="method-name">generate_secret_with_win32_api</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Generate a random secret key by using the Win32 API. Raises LoadError if
the current platform cannot make use of the Win32 API. Raises
SystemCallError if some other error occured.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001598-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001598-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/railties/lib/rails_generator/secret_key_generator.rb, line 28</span>
28:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_secret_with_win32_api</span>
29:       <span class="ruby-comment cmt"># Following code is based on David Garamond's GUID library for Ruby.</span>
30:       <span class="ruby-identifier">require</span> <span class="ruby-value str">'Win32API'</span>
31: 
32:       <span class="ruby-identifier">crypt_acquire_context</span> = <span class="ruby-constant">Win32API</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;advapi32&quot;</span>, <span class="ruby-value str">&quot;CryptAcquireContext&quot;</span>,
33:                                            <span class="ruby-value str">'PPPII'</span>, <span class="ruby-value str">'L'</span>)
34:       <span class="ruby-identifier">crypt_gen_random</span> = <span class="ruby-constant">Win32API</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;advapi32&quot;</span>, <span class="ruby-value str">&quot;CryptGenRandom&quot;</span>,
35:                                       <span class="ruby-value str">'LIP'</span>, <span class="ruby-value str">'L'</span>)
36:       <span class="ruby-identifier">crypt_release_context</span> = <span class="ruby-constant">Win32API</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value str">&quot;advapi32&quot;</span>, <span class="ruby-value str">&quot;CryptReleaseContext&quot;</span>,
37:                                          <span class="ruby-value str">'LI'</span>, <span class="ruby-value str">'L'</span>)
38:       <span class="ruby-identifier">prov_rsa_full</span>       = <span class="ruby-value">1</span>
39:       <span class="ruby-identifier">crypt_verifycontext</span> = <span class="ruby-value">0x</span><span class="ruby-constant">F0000000</span>
40: 
41:       <span class="ruby-identifier">hProvStr</span> = <span class="ruby-value str">&quot; &quot;</span> <span class="ruby-operator">*</span> <span class="ruby-value">4</span>
42:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">crypt_acquire_context</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">hProvStr</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">prov_rsa_full</span>,
43:                                     <span class="ruby-identifier">crypt_verifycontext</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
44:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">SystemCallError</span>, <span class="ruby-node">&quot;CryptAcquireContext failed: #{lastWin32ErrorMessage}&quot;</span>
45:       <span class="ruby-keyword kw">end</span>
46:       <span class="ruby-identifier">hProv</span>, = <span class="ruby-identifier">hProvStr</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">'L'</span>)
47:       <span class="ruby-identifier">bytes</span> = <span class="ruby-value str">&quot; &quot;</span> <span class="ruby-operator">*</span> <span class="ruby-value">64</span>
48:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">crypt_gen_random</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">hProv</span>, <span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">size</span>, <span class="ruby-identifier">bytes</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
49:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">SystemCallError</span>, <span class="ruby-node">&quot;CryptGenRandom failed: #{lastWin32ErrorMessage}&quot;</span>
50:       <span class="ruby-keyword kw">end</span>
51:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">crypt_release_context</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">hProv</span>, <span class="ruby-value">0</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
52:         <span class="ruby-identifier">raise</span> <span class="ruby-constant">SystemCallError</span>, <span class="ruby-node">&quot;CryptReleaseContext failed: #{lastWin32ErrorMessage}&quot;</span>
53:       <span class="ruby-keyword kw">end</span>
54:       <span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-value str">&quot;H*&quot;</span>)[<span class="ruby-value">0</span>]
55:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>